package cn.zmall.manager.service.impl;

import cn.zmall.common.exception.ZmallException;
import cn.zmall.common.pojo.TreeNode;
import cn.zmall.manager.dto.DtoUtil;
import cn.zmall.manager.mapper.TbItemCatMapper;
import cn.zmall.manager.pojo.TbItemCat;
import cn.zmall.manager.pojo.TbItemCatExample;
import cn.zmall.manager.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author BinBini
 * @Date 2020/12/21 11:13
 * @Version 1.0
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public TbItemCat getItemCatById(Long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        if(tbItemCat == null) {
            throw new ZmallException("通过id获得商品分类失败");
        }
        return tbItemCat;
    }

    @Override
    public List<TreeNode> getItemCatList(int parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause("sort_order");
        criteria.andParentIdEqualTo(new Long(parentId));
        List<TbItemCat> itemCatList = tbItemCatMapper.selectByExample(example);

        List<TreeNode> resultList = new ArrayList<>();

        for (TbItemCat tbItemCat : itemCatList) {
            TreeNode treeNode = DtoUtil.TbItemCatToTreeNode(tbItemCat);

            resultList.add(treeNode);
        }

        return resultList;
    }

    @Override
    public int addItemCat(TbItemCat tbItemCat) {
        if(tbItemCat.getParentId() == 0) {
            tbItemCat.setSortOrder(0);
            tbItemCat.setStatus(1);
        }else {
            tbItemCat.setSortOrder(0);
            tbItemCat.setStatus(1);
            tbItemCat.setUpdated(new Date());
            tbItemCat.setCreated(new Date());
        }

        if(tbItemCatMapper.insert(tbItemCat) != 1) {
            throw new ZmallException("添加商品分类失败");
        }
        return 1;
    }

    @Override
    public int updateItemCat(TbItemCat tbItemCat) {
        TbItemCat old = getItemCatById(tbItemCat.getParentId());
        tbItemCat.setUpdated(new Date());
        tbItemCat.setCreated(old.getCreated());

        if(tbItemCatMapper.updateByPrimaryKey(tbItemCat) != 1) {
            throw new ZmallException("更新商品分类失败");
        }
        return 1;
    }

    @Override
    public void deleteItemCat(Long id) {
        deleteTree(id);
    }

    @Override
    public void deleteTree(Long id) {
        //查询该节点所有子节点
        List<TreeNode> node = getItemCatList(Math.toIntExact(id));
        if(node.size() > 0){
            //如果有子节点，遍历子节点
            for(int i = 0; i < node.size(); i++){
                deleteItemCat((long) node.get(i).getId());
            }
        }
        //没有子节点直接删除
        if(tbItemCatMapper.deleteByPrimaryKey(id)!=1){
            throw new ZmallException("删除商品分类失败");
        }
    }
}
